//
//  CNM_ExTextView.swift
//  swiftTXH
//
//  Created by Mac mini on 2020/12/28.
//  Copyright © 2020 Mac mini. All rights reserved.
//

import UIKit

public extension UITextView {
    // MARK: - 设置占位符
    var cnm_placeholder: String? {
        set {
            objc_setAssociatedObject(self, UITextView.Runtimekey.placeholder, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
            // 创建placeholderLabel
            createPlaceholderLabel(cnm_placeholder!)
        }
        get {
            return objc_getAssociatedObject(self, UITextView.Runtimekey.placeholder) as? String
        }
    }
    
    // MARK: - 占位符字体大小
    var cnm_placeholderFont: UIFont? {
        set {
            objc_setAssociatedObject(self, UITextView.Runtimekey.placeholderFont, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            
            if self.cnm_placeholderLabel != nil {
                self.cnm_placeholderLabel?.font = cnm_placeholderFont
            }
        }
        get {
            let font = objc_getAssociatedObject(self, UITextView.Runtimekey.placeholderFont) as? UIFont
            
            if font == nil {
                return UIFont.systemFont(ofSize: 13.0)
            }
            return font
        }
    }
    // MARK: - 占位符颜色
    var cnm_placeholderColor: UIColor? {
        set {
            objc_setAssociatedObject(self, UITextView.Runtimekey.placeholderColor, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            
            if self.cnm_placeholderLabel != nil {
                self.cnm_placeholderLabel?.textColor = cnm_placeholderColor
            }
        }
        get {
            let color = objc_getAssociatedObject(self, UITextView.Runtimekey.placeholderColor) as? UIColor
            if color == nil {
                return UIColor.gray
            }
            return color
        }
    }
    
}

fileprivate extension UITextView {
    // MARK: - 占位符Label
    var cnm_placeholderLabel: UILabel? {
        set {
            objc_setAssociatedObject(self, UITextView.Runtimekey.placeholderLabel, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        get {
            return objc_getAssociatedObject(self, UITextView.Runtimekey.placeholderLabel) as? UILabel
        }
    }
}

// MARK: - 私有
fileprivate extension UITextView {
    // 占位label
    func createPlaceholderLabel(_ text: String) {
        DispatchQueue.main.async {
            self.cnm_placeholderLabel = UILabel()
            self.cnm_placeholderLabel?.text = text
            self.cnm_placeholderLabel?.attributedText = NSAttributedString(string: text, attributes: [.font : self.cnm_placeholderFont!, NSAttributedString.Key.foregroundColor : self.cnm_placeholderColor!])
            self.cnm_placeholderLabel?.numberOfLines = 0
            self.cnm_placeholderLabel?.lineBreakMode = .byWordWrapping
            
            let rect = text.boundingRect(with: CGSize(width: self.cnm_width, height: self.cnm_height), options: .usesLineFragmentOrigin, attributes: [.font : self.cnm_placeholderFont!], context: nil)
            self.cnm_placeholderLabel?.frame = CGRect(x: 5, y: 7, width: rect.size.width, height: rect.size.height)
            self.addSubview(self.cnm_placeholderLabel!)
            
            self.cnm_placeholderLabel?.isHidden = !(self.text.count == 0)
        }
        // 监听输入
        NotificationCenter.default.addObserver(self, selector: #selector(cnm_textChange(_:)), name: UITextView.textDidChangeNotification, object: nil)
    }
}


extension UITextView {
    // runtimeKey
    fileprivate struct Runtimekey {
        static let placeholderLabel: UnsafeRawPointer = UnsafeRawPointer(bitPattern: "PLACEHOLDERLABEL".hashValue)!
        static let placeholder: UnsafeRawPointer = UnsafeRawPointer(bitPattern: "PLACEHOLDER".hashValue)!
        static let placeholderFont: UnsafeRawPointer = UnsafeRawPointer(bitPattern: "PLACEHOLDERFONT".hashValue)!
        static let placeholderColor: UnsafeRawPointer = UnsafeRawPointer(bitPattern: "PLACEHOLDERCOLOR".hashValue)!
    }
    
    // 监听输入文本
    @objc fileprivate func cnm_textChange(_ notification: Notification) {
        let textView = notification.object as! UITextView
        self.text = textView.text
        
        if self.cnm_placeholder != nil {
            self.cnm_placeholderLabel?.isHidden = !(self.text.count == 0)
        }
    }
}
